﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using DevExpress.XtraEditors;

using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.Geodatabase;

namespace SZ
{
    public partial class QueryFrm : DevExpress.XtraEditors.XtraForm
    {
        #region 变量
        public IMap pMap;
        public AxMapControl m_mapControl;
        public string sqlSelect;
        #endregion

        public QueryFrm(AxMapControl mapControl)
        {
            InitializeComponent();
            m_mapControl = mapControl;

        }

        private void QueryFrm_Load(object sender, EventArgs e)
        {
            //字段名
            DevExpress.XtraGrid.Columns.GridColumn colName = new DevExpress.XtraGrid.Columns.GridColumn();
            colName.Name = "colName";
            colName.Caption = "字段名";
            colName.FieldName = "colName";
            colName.VisibleIndex = 0;
            colName.Width = 200;

            this.gridView1.Columns.AddRange(new DevExpress.XtraGrid.Columns.GridColumn[] { colName });

            //字段值
            DevExpress.XtraGrid.Columns.GridColumn colValue = new DevExpress.XtraGrid.Columns.GridColumn();
            colValue.Name = "colValue";
            colValue.Caption = "字段值";
            colValue.FieldName = "colValue";
            colValue.VisibleIndex = 0;
            colValue.Width = 200;

            this.gridView2.Columns.AddRange(new DevExpress.XtraGrid.Columns.GridColumn[] { colValue });

            //将图层名称加载到combox中
            for (int i = 0; i < m_mapControl.LayerCount; i++)
            {
                string layerName = m_mapControl.get_Layer(i).Name;
                layerComboBoxEdit.Properties.Items.Add(layerName);
            }
            layerComboBoxEdit.Text = layerComboBoxEdit.Properties.Items[0].ToString();

            labelControl2.Text = "SELECT * FROM  " + layerComboBoxEdit.Text + "  WHERE:";

            CreateFields(layerComboBoxEdit.Text);
        }

        private void gridControl1_DoubleClick(object sender, EventArgs e)
        {
            sqlSelect = memoEdit1.Text;
            DataRow dataRow = this.gridView1.GetFocusedDataRow();
            string fieldName = dataRow["colName"].ToString();
            sqlSelect = sqlSelect + "\"" + fieldName + "\"";
            memoEdit1.Text = sqlSelect;
        }

        #region 运算符号

        private void equalBtn_Click(object sender, EventArgs e)
        {
            memoEdit1.Text += "=";
        }

        private void notEqualBtn_Click(object sender, EventArgs e)
        {
            memoEdit1.Text += "<>";
        }

        private void likeBtn_Click(object sender, EventArgs e)
        {
            memoEdit1.Text += "like";
        }

        private void greaterBtn_Click(object sender, EventArgs e)
        {
            memoEdit1.Text += ">";
        }

        private void greaterOrEqualBtn_Click(object sender, EventArgs e)
        {
            memoEdit1.Text += ">=";
        }

        private void andBtn_Click(object sender, EventArgs e)
        {
            memoEdit1.Text += "and";
        }

        private void smallerBtn_Click(object sender, EventArgs e)
        {
            memoEdit1.Text += "<";
        }

        private void smallOrEqualBtn_Click(object sender, EventArgs e)
        {
            memoEdit1.Text += "<=";
        }

        private void orBtn_Click(object sender, EventArgs e)
        {
            memoEdit1.Text += "or";
        }

        private void percentBtn_Click(object sender, EventArgs e)
        {
            memoEdit1.Text += "%";
        }

        private void bracketBtn_Click(object sender, EventArgs e)
        {
            memoEdit1.Text += "()";
        }

        private void notBtn_Click(object sender, EventArgs e)
        {
            memoEdit1.Text += "not";
        }

        private void isBtn_Click(object sender, EventArgs e)
        {
            memoEdit1.Text += "is ";
        }


        #endregion

        //获取唯一值（没考虑到空值和重复的值也应该去掉）
        private void onlyValueBtn_Click(object sender, EventArgs e)
        {
            ILayer pSelectLayer;
            IFeatureLayer pSelFeatureLayer;
            IFeatureClass pSelFeatureClass;
            IFeature pFeature;

            DataTable m_dataTable = new DataTable();
            m_dataTable.Columns.Add("colValue", typeof(string));

            for (int i = 0; i < m_mapControl.LayerCount; i++)
            {
                if (m_mapControl.get_Layer(i).Name == layerComboBoxEdit.Text)
                {
                    pSelectLayer = m_mapControl.get_Layer(i);
                    pSelFeatureLayer = pSelectLayer as IFeatureLayer;
                    pSelFeatureClass = pSelFeatureLayer.FeatureClass;

                    //查找字段的索引值
                    DataRow dataRow = this.gridView1.GetFocusedDataRow();
                    string fieldName = dataRow["colName"].ToString();
                    int index = pSelFeatureClass.FindField(fieldName);

                    IFeatureCursor pFeatureCursor = pSelFeatureClass.Search(null, false);
                    pFeature = pFeatureCursor.NextFeature();
                    while (pFeature != null)
                    {
                        DataRow valueDataRow = m_dataTable.Rows.Add();
                        if (pFeature.get_Value(index).ToString() == "")
                        {
                            valueDataRow["colValue"] = "<空>";
                        }
                        else
                        {
                            valueDataRow["colValue"] = pFeature.get_Value(index).ToString();
                        }
                        pFeature = pFeatureCursor.NextFeature();
                    }

                    this.gridView2.GridControl.DataSource = m_dataTable;
                    return;
                }
            }
        }

        private void CreateFields(string layerName)
        {
            //将图层的字段加载到gridControl中
            ILayer pSelectLayer;
            IFeatureLayer pSelFeatureLayer;
            IFeatureClass pSelFeatureClass;
            IFields pFields;

            DataTable m_dataTable = new DataTable();
            m_dataTable.Columns.Add("colName", typeof(string));


            for (int i = 0; i < m_mapControl.LayerCount; i++)
            {
                if (m_mapControl.get_Layer(i).Name == layerName)
                {
                    pSelectLayer = m_mapControl.get_Layer(i);
                    pSelFeatureLayer = pSelectLayer as IFeatureLayer;
                    pSelFeatureClass = pSelFeatureLayer.FeatureClass;
                    pFields = pSelFeatureClass.Fields;

                    DataRow pDataRow = null;

                    for (int j = 0; j < pFields.FieldCount; j++)
                    {
                        string fieldName = pFields.get_Field(j).Name;
                        pDataRow = m_dataTable.Rows.Add();
                        pDataRow["colName"] = fieldName;
                        //是否该将几何字段去除
                    }
                    this.gridView1.GridControl.DataSource = m_dataTable;
                    return;
                }

            }

        }

        private void layerComboBoxEdit_SelectedIndexChanged(object sender, EventArgs e)
        {
            labelControl2.Text = "SELECT * FROM  " + layerComboBoxEdit.Text + "  WHERE:";
            CreateFields(layerComboBoxEdit.Text);
        }

        private void SearchFeatures(string strCondition, IFeatureLayer pFeatureLayer)
        {
            IFeatureSelection pFeatureSelection = pFeatureLayer as IFeatureSelection;
            IQueryFilter pQueryFilter = new QueryFilterClass();
            pQueryFilter.WhereClause = strCondition;
            pFeatureSelection.SelectFeatures(pQueryFilter, esriSelectionResultEnum.esriSelectionResultNew, false);
            m_mapControl.ActiveView.Refresh();
        }

        private void applyBtn_Click(object sender, EventArgs e)
        {
            IFeatureLayer pFeaureLayer;
            for (int i = 0; i < m_mapControl.LayerCount; i++)
            {
                if (m_mapControl.get_Layer(i).Name == layerComboBoxEdit.Text)
                {
                    pFeaureLayer = m_mapControl.get_Layer(i) as IFeatureLayer;
                    SearchFeatures(memoEdit1.Text, pFeaureLayer);
                    return;
                }
            }
        }

        private void gridControl2_DoubleClick(object sender, EventArgs e)
        {
            DataRow pDataRow = this.gridView2.GetFocusedDataRow();
            memoEdit1.Text = memoEdit1.Text + "\'" + pDataRow["colValue"] + "\'";

        }

        private void ensureBtn_Click(object sender, EventArgs e)
        {
            IFeatureLayer pFeaureLayer;
            for (int i = 0; i < m_mapControl.LayerCount; i++)
            {
                if (m_mapControl.get_Layer(i).Name == layerComboBoxEdit.Text)
                {
                    pFeaureLayer = m_mapControl.get_Layer(i) as IFeatureLayer;
                    SearchFeatures(memoEdit1.Text, pFeaureLayer);
                    this.Close();
                    return;
                }
            }
        }

        private void clearBtn_Click(object sender, EventArgs e)
        {
            memoEdit1.Text = "";
        }




    }
}
